﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Watcher.Matrix;
using Watcher.AMQ;
using System.Threading;
using Watcher.AMQ.Objects;
using System.ComponentModel;

namespace Watcher
{
    class Start
    {

        const string TOPIC_NAME = "SampleSubscriptionTopic";
        const string BROKER = "tcp://localhost:61616";
        const string CLIENT_ID = "ActiveMqFirstSubscriber";
        const string CONSUMER_ID = "ActiveMqFirstSubscriber";
        private static MainWindow _form = new MainWindow();
        static Mutex _mutex1;
        static Mutex _mutex2;
        static BindingList<TestData> _dataList;
        static BindingList<string> _messageList = new BindingList<string>();
        public static BindingList<string> MessageList
        {
            get { return _messageList; }
        }
        static DataSimulation _dataSim;
        static SimulationPublisher sp;


        public static void GuiThread()
        {
            
            ThreadStart RunForm = new ThreadStart(startForm);
            Thread formThread = new Thread(RunForm);
            formThread.Start();
            _mutex2.WaitOne();
        }

        public static void SimulationThread()
        {
            ThreadStart RunSimulation = new ThreadStart(_dataSim.StartSimulation);
            Thread simThread = new Thread(RunSimulation);
            simThread.Start();
            _mutex2.WaitOne();
        }

        static void Main(string[] args)
        {
            _dataList = new BindingList<TestData>();
            _dataSim = DataSimulation.GetInstance();
            _mutex1 = new Mutex(true, "simulation");
            _mutex2 = new Mutex(true, "gui");
            sp = new SimulationPublisher("parameters", "tcp://localhost:61616");
            GuiThread();
            SimulationThread();

            try
            {
                using (SubscribeTopic subscriber = new SubscribeTopic("parametersSub", "parameters"))
                {
                    subscriber.OnMessageReceived += new MessageReceivedDelegate(subscriber_OnMessageReceived);
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadKey(true);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            } 
            
        }

        static void subscriber_OnMessageReceived(string message)
        {
            Console.WriteLine(message);
            _messageList.Add(message);

        }

        static void startSimulation()
        {
            while (true)
            {
                TestData td = _dataSim.CreateData();
                _dataList.Add(td);
                sp.SendMessage("Message:" + td.X + " | " + td.Y + " | " + td.Z);
            }
                
        }

        static void startForm()
        {
            Application.Run(_form);
        }
    }
}
